package com.puman.watchtrade.fragment.set.httpHandler;

import java.net.URLEncoder;

import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.Base64;

import com.puman.watchtrade.fragment.home.model.Varietie;
import com.puman.watchtrade.fragment.home.model.Varieties;
import com.puman.watchtrade.fragment.set.model.UserInfo;
import com.puman.watchtrade.util.Config;
import com.puman.watchtrade.util.DataResult;
import com.puman.watchtrade.util.GlobalData;
import com.puman.watchtrade.util.HttpClent;

public class SettingHttpHandler {
	/*
	 *短信 验证码
	 */
	public DataResult valideCode(String mobile, String type) {
		DataResult dataResult = new DataResult();
		String path = Config.baseUrlStr + Config.validateMsgCode 
				+ "mobile="+ mobile
				+"&type="+type;
		String resultJson = HttpClent.getUrlInfo(path);
		JSONObject result;
		try {
			result = new JSONObject(resultJson);
			dataResult.flag = result.getBoolean("flag");
			dataResult.msg = result.getString("msg");
			dataResult.object  = "";
			if (dataResult.flag) {
				JSONObject jsonObject = new JSONObject();
				jsonObject = result.getJSONObject("data");
				dataResult.object = jsonObject.getString("code");
			}
		} catch (JSONException e) {
			e.printStackTrace();
		}
		return dataResult;
	}

	/*
	 * 验证码
	 */
	public DataResult register(String mobile, String psw, String nick) {
		DataResult dataResult = new DataResult();
		String path = Config.baseUrlStr + Config.register 
				+ "mobile="+ mobile
				+"&psw="+psw
				+"&nick="+URLEncoder.encode(nick);
		String resultJson = HttpClent.getUrlInfo(path);
		JSONObject result;
		try {
			result = new JSONObject(resultJson);
			dataResult.flag = result.getBoolean("flag");
			dataResult.msg = result.getString("msg");
		} catch (JSONException e) {
			e.printStackTrace();
		}
		return dataResult;
	}
	
	/*
	 * 重置密码
	 */
	public DataResult resetPass(String mobile, String newPassWord) {
		DataResult dataResult = new DataResult();
		String path = Config.baseUrlStr + Config.resetPass 
				+ "mobile="+ mobile
				+"&newPassWord="+newPassWord;
		String resultJson = HttpClent.getUrlInfo(path);
		JSONObject result;
		try {
			result = new JSONObject(resultJson);
			dataResult.flag = result.getBoolean("flag");
			dataResult.msg = result.getString("msg");
		} catch (JSONException e) {
			e.printStackTrace();
		}
		return dataResult;
	}
	
	/*
	 * 登录
	 */
	public DataResult login(String mobile, String PassWord) {
		DataResult dataResult = new DataResult();
		String path = Config.baseUrlStr + Config.login 
				+ "mobile="+ mobile
				+"&psw="+PassWord
				+"&device_no="+GlobalData.deviceNo;
		String resultJson = HttpClent.getUrlInfo(path);
		JSONObject result;
		try {
			result = new JSONObject(resultJson);
			dataResult.flag = result.getBoolean("flag");
			dataResult.msg = result.getString("msg");
			if (dataResult.flag) {
				GlobalData.userInfo = new UserInfo();
				JSONObject jsonObject = result.getJSONObject("data");
				GlobalData.userInfo.setAddress(jsonObject.getString("address"));
				GlobalData.userInfo.setAuctioningCount(jsonObject.getString("auctioningCount"));
				GlobalData.userInfo.setCityId(jsonObject.getString("cityId"));
				GlobalData.userInfo.setEmail(jsonObject.getString("email"));
				GlobalData.userInfo.setFeature(jsonObject.getString("feature"));
				GlobalData.userInfo.setHeadPicture(Config.imgUrl+jsonObject.getString("headPicture"));
				GlobalData.userInfo.setMobile(jsonObject.getString("mobile"));
				GlobalData.userInfo.setName(jsonObject.getString("name"));
				GlobalData.userInfo.setNick(jsonObject.getString("nick"));
				GlobalData.userInfo.setNotWinCount(jsonObject.getString("notWinCount"));
				GlobalData.userInfo.setProviceID(jsonObject.getString("proviceID"));
				if (jsonObject.getString("sex").equals("0")) {
					GlobalData.userInfo.setSex("男");
				}else if (jsonObject.getString("sex").equals("1")) {
					GlobalData.userInfo.setSex("女");
				}else {
					GlobalData.userInfo.setSex("不确定");
				} 
				GlobalData.userInfo.setTradedCount(jsonObject.getString("tradedCount"));
				GlobalData.userInfo.setType(jsonObject.getString("type"));
				GlobalData.userInfo.setUserNo(jsonObject.getString("userNo"));
				GlobalData.userInfo.setWinCount(jsonObject.getString("winCount"));
				String poundage = jsonObject.getString("commission_ratio");
				poundage = poundage.replace("%", "");
				GlobalData.userInfo.setPoundage(poundage);
			}
		} catch (JSONException e) {
			e.printStackTrace();
		}
		return dataResult;
	}
	
	/*
	 * 登出
	 */
	public DataResult loginOut() {
		DataResult dataResult = new DataResult();
		if (GlobalData.userInfo == null) {
			return dataResult;
		}
		String path = Config.baseUrlStr + Config.loginOut 
				+ "user_no="+ GlobalData.userInfo.getUserNo();
		String resultJson = HttpClent.getUrlInfo(path);
		JSONObject result;
		try {
			result = new JSONObject(resultJson);
			dataResult.flag = result.getBoolean("flag");
			dataResult.msg = result.getString("msg");
		} catch (JSONException e) {
			e.printStackTrace();
		}
		return dataResult;
	}

}
